package com.ran.controller;

import com.alibaba.fastjson.JSON;
import com.mysql.cj.xdevapi.JsonArray;
import com.ran.entity.employee;
import com.ran.service.empService;
import com.ran.service.serviceImpl.empServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

/**
 *表现层
 *获取前端传入的数据
 * 完成员工的登录注册功能
 * 完成员工的crud功能
 */
@WebServlet("/employee")
public class Employee extends HttpServlet {
    private empService empservice = new empServiceImpl();
    boolean flag = false;

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置请求与响应的格式
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("application/json;charset=UTF-8");
        //根据获取的method采取相应的操作
        String act = req.getParameter("act");
//        System.out.println(act);
        switch (act){
//            登录功能
            case "login":
                String ename=req.getParameter("ename");
                String epsd=req.getParameter("epsd");
                //将业务交给service完成
                try {
                    flag=this.empservice.login(ename,epsd);
//                    System.out.println(flag);
                } catch (SQLException e) {
                    System.out.println("Login Default!");
                    e.printStackTrace();
                }
//                成功则重定向到首页,否则重新进入登陆界面并弹出提示
                if(flag){
                    resp.sendRedirect("home.jsp");
                }else{
                    req.getRequestDispatcher("login.jsp?flag=false").forward(req,resp);
                }
                break;
//                注册、添加新员工功能
            case "register":
//                获取前端传来的数据
                String reename = req.getParameter("ename");
                int egendar = Integer.parseInt(req.getParameter("egendar"));
                int depid = Integer.parseInt(req.getParameter("depid"));
//                将数据封装进对象并交给service处理
                try {
                    flag=this.empservice.register(new employee(reename,egendar,depid));
                } catch (Exception e) {
                    System.out.println("添加员工信息出错了!--controller");
                    e.printStackTrace();
                }
//                根据flag向前端传递提示信息
                if(flag){
                    req.getRequestDispatcher("register.jsp?flag=true").forward(req,resp);
                }else{
                    req.getRequestDispatcher("register.jsp?flag=false").forward(req,resp);
                }
                break;
//                展示所有员工功能
            case "allemp":
//                System.out.println("成功进入allemp!");
//                req.setAttribute("list",this.empservice.allemp());
//                req.getRequestDispatcher("home.jsp").forward(req,resp);
                List list = this.empservice.allemp();
                String jsonStr = JSON.toJSONString(list);
                resp.getWriter().write(jsonStr);
                break;
//                产看特定员工信息
            case "search":
//                接收home.jsp的search传来的id，查询到的结果返回给home.jsp,home.jsp将数据打包发给getdata,getdata再将数据传入update.jsp作为默认值（同时渲染观看）
                int empid = Integer.parseInt(req.getParameter("empid"));
//                System.out.println("controller-search接收的参数："+empid);   //参数接收成功
                List searchlist = this.empservice.search(empid);
                String jsonstr = JSON.toJSONString(searchlist.get(0));
                System.out.println("controller-search接收到的返回值："+jsonstr);//因为没有接收empid，故返回的empid均为0
                resp.getWriter().write(jsonstr);
                break;
//                修改员工功能
            case"update":
//                获取数据
                String enameup = req.getParameter("ename");
                int egendarup = Integer.parseInt(req.getParameter("egendar"));
                int depidup = Integer.parseInt(req.getParameter("depid"));
                int empidup = Integer.parseInt(req.getParameter("empid"));
                flag=this.empservice.update(new employee(empidup,enameup,egendarup,depidup));
//                根据flag向前端传递提示信息
                if(flag){
                    req.getRequestDispatcher("update.jsp?flag=true").forward(req,resp);
                }else{
                    req.getRequestDispatcher("update.jsp?flag=false").forward(req,resp);
                }
                break;
//                删除某一员工信息
            case "delete":
                int empidde = Integer.parseInt(req.getParameter("empid"));
                flag=this.empservice.delete(empidde);
                if(flag){
                    req.getRequestDispatcher("home.jsp?flag=true").forward(req,resp);
                }else{
                    req.getRequestDispatcher("home.jsp?flag=false").forward(req,resp);
                }
                break;

        }
    }
}
